package com.hall.controller;


import com.hall.pojo.Appointment;
import com.hall.pojo.Dbdict;
import com.hall.pojo.SysUser;
import com.hall.pojo.common.Result;
import com.hall.service.AttentionServes;
import com.hall.service.HallService;
import com.hall.service.UserService;
import com.hall.utils.UserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * 会诊大厅
 */

@Controller
@RequestMapping("/hall")
public class HallController {
    @Autowired
    private HallService hallService;
    @Autowired
    private AttentionServes attentionServes2;
    @Autowired
    private UserService userService;

    @RequestMapping("/allHall")
    public String list(Model model,HttpServletRequest request2) {
        //非医助管理员身份默认获取会诊大厅全部会议列表
        List<Appointment> list;
        SysUser sysUser2 = UserSession.getSysUser(request2);
        //会诊大厅
        list = hallService.queryAllDoctor();
        Iterator<Appointment> iterator = list.iterator();
        while (iterator.hasNext()) {
            boolean data = iterator.next().getApplyTime()==null;
            if (data) {
                iterator.remove();
            }
        }
        model.addAttribute("list", list);
        //获取省市级下拉列表
        List<Dbdict> dbdictList = hallService.querySurface();
        //获取科室列表
        model.addAttribute("dbdictList", dbdictList);
        return "allHall";
    }
    //随机推荐三场会诊
    @ResponseBody
    @GetMapping("/recommend/{id}")
    private Result<List<Appointment>> recod(@PathVariable String id){
        List<Appointment> list = new ArrayList<>();
        list=hallService.queryAllDoctor();  //所有查询的会诊集合
        System.out.println(list);
        if(list.size()<1){
            return Result.error("无会诊"); //如果会诊表为空,直接返回
        }
        List<Appointment> newList = new ArrayList<>();      //推荐的集合
        Iterator<Appointment> iterator = list.iterator();  //去掉空会诊
        while (iterator.hasNext()) {
            boolean data =iterator.next().getApplyTime()==null;
            if (data) {
                iterator.remove();
            }
        }
        if(list.size()<=3){      //如果全部会诊小于3条,不随机,全部返回
            return Result.success(list,"获取成功");
        }else{        //大于3条数据
            Random random = new Random();
            for(int i = 0;i<3;i++){
                int ram = random.nextInt(list.size()-1);  //随机取3条
                newList.add(list.get(ram));    //放入新集合
                list.remove(list.get(ram));   //清空原集合
            }
            return Result.success(newList,"请求成功");
        }
    }

    //查询医生
    @RequestMapping("queryDoctor")
    public String queryDoctor(String doctorName, Model model) {
        List<Appointment> list = hallService.queryDoctor(doctorName);
        model.addAttribute("list", list);
        return "allHall";
    }

    //下拉
    @RequestMapping("/querySurface")
    public String dbdictList(Model model) {
        List<Dbdict> dbdictList = hallService.querySurface();
        if (dbdictList != null)
            model.addAttribute("dbdictList", dbdictList);
        return "allHall";
    }

    //收藏功能
    @ResponseBody
    @RequestMapping("/addCollect")
    //获取当前登录用户对象id
    public Result addCollect(String appointId, HttpSession session, HttpServletRequest request) {
        SysUser sysUser = UserSession.getSysUser(request);
        if ("".equals(sysUser.getId()) | sysUser.getId().length() <= 0) {
            return Result.error("未登录");
        }
        int i = 0;
        String Id = sysUser.getId(); //获取当前登录用户id
        //查询当前id收藏的所有会议
        List<String> is = attentionServes2.isCollect(Id);
        if (!is.contains(appointId)) { //如果收藏过则不执行二次收藏
            //再判断是否存在状态为0的当前会诊,如果有直接修改状态,没有则插入新收藏数据
            if (attentionServes2.pastselect(appointId, Id) > 0) {
                //执行修改操作
                i = attentionServes2.updatecollect(appointId, Id);
            } else {
                i = attentionServes2.addCollect(appointId, Id);
            }
            if (i > 0) {
                return Result.success(null,"收藏成功");
            }
        }
        return Result.error("当前会议已收藏过");
    }

    //正在直播功能
    @RequestMapping("/meetLive")
    public String meetLive(Model md5, HttpSession session, HttpServletRequest request) {
        SysUser user = UserSession.getSysUser(request);
        /*if (user.getId() == null) {
            return "return_login";
        }*/
        List<Appointment> ap6 = hallService.allNowMeeting();
        List<Appointment> ap7 = hallService.allAfterMeeting();
        md5.addAttribute("loginUser", userService.getUser(user.getId()));
        md5.addAttribute("listMeetNow", ap6);
        md5.addAttribute("listMeetAfter", ap7);
        return "live_meeting";
    }

    //正在直播详情页
    @RequestMapping("/meetLiveShow")
    public String meetLiveShow(WebRequest request2, Model model4, HttpServletRequest request) {
        //获取url中的分享者码
        String code = request2.getParameter("invitecode");
        //获取url中当前直播链接
        String url = request2.getParameter("liveId");
        //将当前分享的url链接中的直播地址渲染到页面ifram展示
        model4.addAttribute("liveId", url);
        if (code == null || "".equals(code)) {
            //这里功能:如果分享者码为空则证明是自己点击直播链接进来看的,给自己加积分
            //思考:自己点进来看直播,怎么控制同一场直播只有第一次看的时候加积分(如果难以实现则限制一个用户一天通过直播模块可以加的积分总量)

        }
        if (code != null) {
            //这里功能:如果分享码不为空则证明是通过别人分享的链接访问的,给自己加积分,给分享人加积分
            //思考:怎么限制一场直播分享给一个人的时候怎么让做到让观看者只是第一次看的时候给分享者和自己加积分(不能通过重复进入分享的直播链接反复进入刷积分)ps:(如果难以实现则限制一个用户一天通过直播模块可以加的积分总量)
        }
        return "live_meetingshow";
    }
    @ResponseBody
    @GetMapping("/userIdList/{id}")
    private Result<List<String>>participateUser(@PathVariable String id, HttpServletRequest request2){
        String uid = UserSession.getSysUser(request2).getId();
        Boolean utp = "assistant".equals(UserSession.getSysUser(request2).getUsertype());
        if(utp){
            return Result.success(null,null);
        }
        List<String> li = hallService.participateUser(id);
        if(li.contains(uid)){
            return Result.success(null,null);
        }else{
            return Result.error(null);
        }
    }
}
